import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:gold/base/provider_widget.dart';
import 'package:gold/http/api_gold.dart';
import 'package:gold/model/gold_recover_order_model.dart';
import 'package:gold/model/gold_recover_shop_model.dart';
import 'package:gold/ui/my/order/recover/offline_detail_viewmodel.dart';
import 'package:gold/ui/my/order/recover/recover_config.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_size.dart';
import 'package:gold/widgets/common_widgets.dart';
import 'package:gold/widgets/xfg_text.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
import 'package:url_launcher/url_launcher.dart';

/// @description: 线下订单详情
/// @author tangzhentao
/// @created at 23:01 2025/3/5

class OfflineDetailPage extends StatelessWidget {
  /// 回收订单详情
  final GoldRecoverOrderModel order;

  const OfflineDetailPage({super.key, required this.order});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: xfgAppbar(title: '详情'),
      body: Padding(
        padding: EdgeInsets.fromLTRB(XfgSize().size10, XfgSize().size10, XfgSize().size10, 0),
        child: Column(
          children: [
            Container(
              decoration: white10Decoration,
              child: Column(
                children: [
                  Container(
                    height: XfgSize().size49,
                    padding: EdgeInsets.symmetric(horizontal: XfgSize().size10),
                    alignment: Alignment.centerLeft,
                    child: XfgText('黄金料 ${order.weight?? '--'}g', fontSize: XfgSize().text16, color: XfgColor.text1, bold: FontWeight.bold),
                  ),
                  strokeHeightContainer,
                  Padding(
                    padding: EdgeInsets.fromLTRB(XfgSize().size12, XfgSize().size18, 0, XfgSize().size18),
                    child: Column(
                      children: [
                        /// 金价，预估总价
                        Row(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: [
                            Expanded(
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  crossAxisAlignment: CrossAxisAlignment.end,
                                  children: [
                                    XfgText('金价', fontSize: XfgSize().text14, color: XfgColor.text1D, bold: FontWeight.bold),
                                    SizedBox(width: XfgSize().size8),
                                    XfgText('¥', fontSize: XfgSize().text12, color: XfgColor.text1D, bold: FontWeight.bold),
                                    ...getRecoverMoneyWidgets(order.price, XfgColor.text1D)
                                  ],
                                )
                            ),
                            Expanded(
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  crossAxisAlignment: CrossAxisAlignment.end,
                                  children: [
                                    XfgText('预估总价', fontSize: XfgSize().text14, color: XfgColor.theme, bold: FontWeight.bold),
                                    SizedBox(width: XfgSize().size8),
                                    XfgText('¥', fontSize: XfgSize().text12, color: XfgColor.theme, bold: FontWeight.bold),
                                    ...getRecoverMoneyWidgets(order.totalPrice, XfgColor.theme)
                                  ],
                                )
                            ),
                          ],
                        ),
                        Container(
                          margin: EdgeInsets.only(top: XfgSize().size18),
                          alignment: Alignment.centerLeft,
                          child: XfgText('预约时间：${order.arriveTime?? '--'}', fontSize: XfgSize().text14, color: XfgColor.text2),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(XfgSize().size10, XfgSize().size20, 0, XfgSize().size10),
              alignment: Alignment.centerLeft,
              child: XfgText('附近门店', fontSize: XfgSize().text16, color: XfgColor.text3),
            ),
            Expanded(
              child: ProviderWidget<OfflineDetailViewmodel>(
                model: OfflineDetailViewmodel(ApiGold.recycleStore),
                onInit: (data) => data.getLocation(),
                builder: (ctx, detail) => SmartRefresher(
                  controller: detail.controller,
                  enablePullUp: true,
                  enablePullDown: true,
                  onRefresh: () => detail.refresh(),
                  onLoading: () => detail.loadMore(),
                  child: ListView.builder(
                    itemCount: detail.list.length,
                    itemBuilder: (_, index) {
                      final model = detail.list[index];
                      final isLast = detail.list.length == index + 1;
                      final address = _getAddress(model);
                      return Container(
                        margin: EdgeInsets.fromLTRB(0, index == 0 ? 0:XfgSize().size10, 0, isLast ? XfgSize().bottomBarHeight:0),
                        padding: EdgeInsets.symmetric(horizontal: XfgSize().size10, vertical: XfgSize().size18),
                        decoration: white6Decoration,
                        child: Column(
                          children: [
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                XfgText(model.name?? '', fontSize: XfgSize().text18, color: XfgColor.text1D, bold: FontWeight.bold),
                                XfgText('营业时间：${_getTime(model.workingTime, model.closingTime)}', fontSize: XfgSize().text14, color: XfgColor.text3)
                              ],
                            ),
                            SizedBox(height: XfgSize().size14),
                            Row(
                              children: [
                                XfgText('商家电话： ${model.contactMobile??''} ${model.contactName??''}', fontSize: XfgSize().text14, color: XfgColor.text3),
                                const Spacer(),
                                Visibility(
                                  visible: model.contactMobile?.isNotEmpty == true,
                                  child: GestureDetector(
                                    onTap: () async {
                                      final uri = Uri.parse('tel:${model.contactMobile?? ''}');
                                      if (await canLaunchUrl(uri)) {
                                        await launchUrl(uri);
                                      }
                                    },
                                    child: Icon(Icons.phone, size: XfgSize().size18, color: XfgColor.shopListReserve),
                                  ),
                                )
                              ],
                            ),
                            SizedBox(height: XfgSize().size10),
                            Row(
                              children: [
                                XfgText('商家地址：$address', fontSize: XfgSize().text14, color: XfgColor.text3),
                                const Spacer(),
                                Visibility(
                                  visible: address.isNotEmpty,
                                  child: Builder(builder: (con) {
                                    return GestureDetector(
                                      onTap: () {
                                        Clipboard.setData(ClipboardData(text: address)).then((_) => xfgShowSnackBar(con, '商家地址', address));
                                      },
                                      child: XfgText('复制', fontSize: XfgSize().text14, color: XfgColor.shopListReserve),
                                    );
                                  }),
                                )
                              ],
                            ),
                            Visibility(
                              visible: order.shopId == model.id,
                              child: Container(
                                margin: EdgeInsets.only(top: XfgSize().size10),
                                alignment: Alignment.centerLeft,
                                child: XfgText('已预约', fontSize: XfgSize().text14, color: XfgColor.theme),
                              ),
                            )
                          ],
                        ),
                      );
                    },
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  /// 获取展示地址
  String _getAddress(GoldRecoverShopModel model) {
    String address = '';
    if (model.province?.isNotEmpty == true) {
      address += model.province!;
    }
    if (model.city?.isNotEmpty == true) {
      address = address + (address.isNotEmpty ? '-':'') + model.city!;
    }
    if (model.area?.isNotEmpty == true) {
      address = address + (address.isNotEmpty ? '-':'') + model.area!;
    }
    if (model.address?.isNotEmpty == true) {
      address = address + (address.isNotEmpty ? '-':'') + model.address!;
    }
    return address;
  }

  /// 获取营业时间
  String _getTime(String? start, String? end) {
    if (start?.isNotEmpty == true && end?.isNotEmpty == true) {
      return '$start-$end';
    }
    return '--';
  }
}
